﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using DAL.DBHelpers;
namespace QuanLyKhachHang
{
    public partial class uc_SanPham : UserControl
    {
        public uc_SanPham()
        {
            InitializeComponent();
        }
        string Id = string.Empty;
        private void uc_SanPham_Load(object sender, EventArgs e)
        {
            loaddata();
            loadccbLoaiSP();
        }
        public void loaddata()
        {
            var dt = DBHelper.GetDataTableSP("sp_SanPham_Get");
            gdSP.DataSource = dt;
        }
        public void loadccbLoaiSP()
        {
            var dt = DBHelper.GetDataTableSP("sp_LoaiSP_getAllLoaiSP");
            cbLoaiSP.DataSource = dt;
            cbLoaiSP.DisplayMember = "LoaiSP";
            cbLoaiSP.ValueMember = "Id";
        }
        public void resetfield()
        {
            txtGia.Text = "";
            txtMaSP.Text = "";
            txtSL.Text = "";
            txtTenSp.Text = "";
            txtTT.Text = "";
          
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            string msp = string.Empty;
            if (DBHelper.CheckExitInDB(txtMaSP.Text,"MaSP","sp_SanPham_Get") == true)
            {
                MessageBox.Show("Đã tồn tại Mã sản phẩm này !");
                return;
            }
            else msp = txtMaSP.Text;
            ls.Add(new SqlParameter("@MaSP",msp));
            string lsp = cbLoaiSP.SelectedValue.ToString();
            ls.Add(new SqlParameter("@LoaiSPId",lsp));
            ls.Add(new SqlParameter("@TenSP",txtTenSp.Text));
            ls.Add(new SqlParameter("@ThongTin",txtTT.Text));
            ls.Add(new SqlParameter("@Gia",txtGia.Text));
            ls.Add(new SqlParameter("@SoLuong",txtSL.Text));
            Boolean tth;
            if (cbTrangThai.Checked) tth=true;
            else tth=false;
            ls.Add(new SqlParameter("@TrangThai",tth));
            DBHelper.ExecuteNonQuery("sp_SanPham_Add", ls);
            loaddata();
            resetfield();
            MessageBox.Show("Thêm thành công!");
        }

        private void BtnEdit_Click(object sender, EventArgs e)
        {

            List<SqlParameter> ls = new List<SqlParameter>();
            string msp = string.Empty;
            msp = txtMaSP.Text;
            ls.Add(new SqlParameter("@MaSP", msp));
            string lsp = cbLoaiSP.SelectedValue.ToString();
            ls.Add(new SqlParameter("@LoaiSPId", lsp));
            ls.Add(new SqlParameter("@TenSP", txtTenSp.Text));
            ls.Add(new SqlParameter("@ThongTin", txtTT.Text));
            ls.Add(new SqlParameter("@Gia", txtGia.Text));
            ls.Add(new SqlParameter("@SoLuong", txtSL.Text));
            Boolean tth;
            if (cbTrangThai.Checked) tth = true;
            else tth = false;
            ls.Add(new SqlParameter("@TrangThai", tth));
            DBHelper.ExecuteNonQuery("sp_SanPham_Update", ls);
            loaddata();
            resetfield();
            MessageBox.Show("Cập nhật thành công!");
        }

        private void BtnDelete_Click(object sender, EventArgs e)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id",Id));
            DBHelper.ExecuteNonQuery("sp_SanPham_Delete", ls);
            loaddata();
            resetfield();
            MessageBox.Show("Xoá Thành công !");
        }

        private void BtnExport_Click(object sender, EventArgs e)
        {
            gdSP.ShowPrintPreview();
        }

        private void gdSP_Click(object sender, EventArgs e)
        {

            txtMaSP.Enabled = false;
            Id = gdvNV.GetFocusedRowCellDisplayText(col_ID);
            txtTT.Text = gdvNV.GetFocusedRowCellDisplayText(col_TT);
            txtTenSp.Text = gdvNV.GetFocusedRowCellDisplayText(col_TenSP);
            txtSL.Text = gdvNV.GetFocusedRowCellDisplayText(col_SL);
            txtMaSP.Text = gdvNV.GetFocusedRowCellDisplayText(col_MaSP);
            txtGia.Text = gdvNV.GetFocusedRowCellDisplayText(col_Gia);
            string LoaiSP = gdvNV.GetFocusedRowCellDisplayText(col_LoaiSP);
            DataTable dt = new DataTable();
            List<SqlParameter> l = new List<SqlParameter>();
            l.Add(new SqlParameter("@MaSP", gdvNV.GetFocusedRowCellDisplayText(col_MaSP)));
            dt = DBHelper.GetDataTableSP("sp_SanPham_Get", l);
            string lsp = dt.Rows[0]["LoaiSPId"].ToString();
            cbLoaiSP.Controls.Clear();
            cbLoaiSP.SelectedValue = lsp;
            string tth = gdvNV.GetFocusedRowCellDisplayText(col_Trangthai);
            if (tth == "Checked") cbTrangThai.Checked = true;
            else cbTrangThai.Checked = false;
           
        }
    }
}
